package 递归;

public class Factorial {
	
	public static void main(String[] args) {
		System.out.println(factorial1(4));
	}
	
	public static int factorial(int n) {
		if (n <= 1) return 1;
		return n * factorial(n - 1);
	}
	
	/**
	 * 尾递归优化
	 * @param n
	 * @return
	 */
	public static int factorial1(int n) {
		return factorial1(n, 1);
	}
	
	public static int factorial1(int n, int result) {
		if (n <= 1) return result;
		return factorial1(n - 1, n * result);
	}
}
